<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2023/5/17
  Time: 16:48
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%String path = request.getContextPath()+"/";%>
<!doctype html>
<html>
<head>
    <base href="<%=path%>">
    <title>Title</title>
    <link rel="stylesheet" href="hplus/static/ztree/css/zTreeStyle/zTreeStyle.css">
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
    <script type="text/javascript" src="hplus/static/jquery-3.6.3.min.js"></script>
    <script type="text/javascript" src="hplus/static/ztree/js/jquery.ztree.core.min.js"></script>
    <script type="text/javascript" src="hplus/static/ztree/js/jquery.ztree.excheck.js"></script>
    <script src="hplus/static/bootstrap-table.min.js"></script>

</HEAD>
<BODY>
<div style="text-align: center">
    <div class="panel panel-primary">
        <div class="panel-heading">
            <button type="button" onclick="expand(true);" class="btn btn-warning"><i class="glyphicon glyphicon-saved"></i>展开</button>
            <button type="button" onclick="expand(false);" class="btn btn-warning"><i class="glyphicon glyphicon-saved"></i>折叠</button>
            <button type="button" onclick="checkNodes(true);" class="btn btn-warning"><i class="glyphicon glyphicon-saved"></i>全选</button>
            <button type="button" onclick="checkNodes(false);" class="btn btn-warning"><i class="glyphicon glyphicon-saved"></i>取消</button>


        </div>
        <div class="panel-body">
        <div id="treeDemo" class="ztree">
        </div>
        </div></div>

    <button type="button" onclick="save();" class="btn btn-primary"><i class="glyphicon glyphicon-saved"></i>保存</button>
</div>
</BODY>
<script src="hplus/js/plugins/layer/layer.min.js" type="text/javascript"></script>
<script src="hplus/js/sofwin.js"></script>
<SCRIPT LANGUAGE="JavaScript">
    var zTreeObj;
    // zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
    // 对树进行配置的  能不有复选框
    function zTreeOnCheck(event, treeId, treeNode) {
        //alert(treeNode.tId + ", " + treeNode.name + "," + treeNode.checked);
    };
    var setting = {
        data:{
            simpleData:{
                enable: true,// 简单格式的json数据
                idKey: 'id',
                pIdKey:'parentId',
                rootPId: 0   // 第一层节点的PID的值
            }
        },
        check:{
            enable: true
        },
        callback: {
            // 当单选框或复选框被勾选或取消勾选时进行的回调
            onCheck: zTreeOnCheck
        }
    };
    // zTree 的数据属性，深入使用请参考 API 文档（zTreeNode 节点数据详解）
    // 数组就是我们的节点数据
    //
    var zNodes = [
        {name:"test1", open:true, children:[
                {name:"test1_1"}, {name:"test1_2"}]},
        {name:"test2", open:false, children:[
                {name:"test2_1"}, {name:"test2_2"}]}
    ];
    var zNodes1;
    $.operator.get('resource?roleId=${roleId}',function(ret){
        zNodes1=ret.data;


    })
    zTreeObj = $.fn.zTree.init($("#treeDemo"), setting, zNodes1);
    function getCheckedNodes(){
        // 来获取当前选中的所有节点
    var checkedNodes = zTreeObj.getCheckedNodes(true);
    console.info(checkedNodes)
    }
    function expand(flag){
        zTreeObj.expandAll(flag);
    }
    function checkNodes(flag){
        zTreeObj.checkAllNodes(flag);
    }
    function save(){
        // 将获取到的所有的节点的ID传入到后端，将角色ID也传入后端
        var checkedNodes = zTreeObj.getCheckedNodes(true);
        var ids = new Array();
        for(var i=0;i<checkedNodes.length;i++){
            ids.push(checkedNodes[i].id);
        }
        // ids 资源的id
        // 角色id
        var roleId='${roleId}';
        $.operator.post('role/savePermission',{ids:ids,roleId:roleId},function(ret){
            // stringify 将json对象转为json格式的字符串
           if(ret.status){
               layer.msg('权限保存成功',function(){
                   parent.layer.closeAll();
               })
           }
        })
    }
</SCRIPT>
</HTML>
